package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IOfflineEventsDao;
import com.magicode.cms.dao.IOfflineEventsUserDao;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.OfflineEvents;
import com.magicode.cms.entiy.base.OfflineEventsUser;
import com.magicode.cms.entiy.base.OfflineEventsUserBuyHistory;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.service.IOfflineEventsService;
import com.magicode.cms.service.IOfflineEventsUserService;
@Service("serviceOfflineEventsUser")
public class OfflineEventsUserServiceImpl extends BaseServiceImpl<OfflineEventsUser,IOfflineEventsUserDao> implements IOfflineEventsUserService,UtilPath {

	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +OfflineEventsUser.class.getName()+" "+entity_alias_name+" "
					+ " left join t.user as u "
					+ " left join t.offlineEvents as o "
					+ " left join t.applyStatus as s " ;

		if(null!=map && !map.isEmpty()){

			if(map.containsKey("fetchPayType")){
				hql += " left outer join fetch t.payType  ";
			}
			
		}
				hql += " where 1 = 1";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
			if(map.containsKey("applyStatusId")){
				hql = hql+" and "+entity_alias_name+".applyStatus.id = '"+map.get("applyStatusId")+"'";
			}
			if(map.containsKey("offlineId")){
				hql = hql+" and "+entity_alias_name+".offlineEvents.id = '"+map.get("offlineId")+"'";
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
	//	报名人姓名、用户昵称（链接至用户个人中心）、报名时间、手机号、邮箱、报名人数、报名状态、留言、操作（修改、删除）
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "userName");
		map.put(json_title, "报名人姓名");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "loginName");
		map.put(json_title, "用户昵称");
		map.put("url",getCtx().getContextPath()+"/user");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "applyTime");
		map.put(json_title, "报名时间");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "mobile");
		map.put(json_title, "手机号");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "email");
		map.put(json_title, "邮箱");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "attendCount");
		map.put(json_title, "报名人数");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "applyStatus");
		map.put(json_title, "报名状态");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "comments");
		map.put(json_title, "留言");
		map.put("width", 90);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public IOfflineEventsUserDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return offlineEventsUserDao;
	}
	@Autowired
	@Qualifier("daoOfflineEventsUser")
	private IOfflineEventsUserDao offlineEventsUserDao;
	
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("userName", ""+entity_alias_name+".userName as userName ");
			gridsel.put("loginName", "u.loginName as loginName ");
			gridsel.put("applyTime", ""+entity_alias_name+".applyTime as applyTime ");
			gridsel.put("mobile", ""+entity_alias_name+".mobile as mobile ");
			gridsel.put("email", ""+entity_alias_name+".email as email ");
			gridsel.put("attendCount", ""+entity_alias_name+".attendCount as attendCount ");
			gridsel.put("applyStatus", "s.title as applyStatus ");
			gridsel.put("comments", ""+entity_alias_name+".comments as comments ");
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	
	/**
	 * 添加一个参加用户
	 * @param offlineEventsId
	 * @param userId
	 * @throws Exception 
	 */
	@Override
	public void addAttendUser(String offlineEventsId , String userId) throws Exception {
		OfflineEvents ole = new OfflineEvents();
		ole.setId(offlineEventsId);
//		(OfflineEvents) offlineEventsDao.getObject(OfflineEvents.class.getName(), offlineEventsId);
		addAttendUser(ole , userId);
		
	}
	@Override
	public void addAttendUser( OfflineEvents offlineEvents , String userId){
		OfflineEventsUser oleUser = new OfflineEventsUser();
		User user = new User();
		user.setId(userId);
		oleUser.setOfflineEvents(offlineEvents);
		oleUser.setApplyTime(new Date());
		oleUser.setUser(user);
//		申请状态暂时不设置
//		oleUser.setApplyStatus(applyStatus);
		this.createObject(oleUser);
		this.genAttendUserCount(oleUser.getId());
	}
	/**
	 * 删除一个参加用户
	 * @param offlineEventsId
	 * @param userId
	 * @throws Exception 
	 */
	@Override
	public void deleteAttendUser(String offlineEventsId, String userId)  {
		// TODO Auto-generated method stub
		String hql = "";
		OfflineEventsUser oleu = null;
		try {
			List<OfflineEventsUser> list = this.queryDataList(hql, 0, 0);
			if(list != null && list.size() > 0){
				oleu = list.get(0);
			}
			this.offlineEventsUserDao.deleteObject(oleu);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	/**
	 * 生成参加用户数量
	 * @param offlineEventsId
	 * @param userId
	 */
	@Override
	public void genAttendUserCount(String offlineEventsId) {
		String hql = " select  sum(t.attendCount) from com.magicode.cms.entiy.base.OfflineEventsUser t "
				+ " where t.offlineEvents.id = '"+offlineEventsId+"'  ";
		Long sumCount = new Long(0);
		List countList = null;
		try {
			countList = this.queryDataList(hql);
			if(countList != null && countList.size() > 0){
				sumCount = (Long) countList.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		OfflineEvents oe = null;
		try {
			oe = (OfflineEvents) offlineEventsUserDao.getObject(OfflineEvents.class.getName(), offlineEventsId);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(oe != null){
			oe.setAttendUserCount(sumCount.intValue());
			try {
				this.offlineEventsUserDao.editObject(oe);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * 得到用户购买状态
	 * @throws Exception 
	 */
	
	@Override
	public Dictionary getUserBuyStatus(String offUserId) throws Exception {
		// TODO Auto-generated method stub
		Dictionary result = null;
		String hql = "select t.status from "+OfflineEventsUserBuyHistory.class.getName()+" t where t.offlineEventsUser.id = :id ";
		Map map = new HashMap();
		map.put("id", offUserId);
		List<Dictionary> list = this.offlineEventsUserDao.getCommonDaoService().queryList(hql, map);
		if(list != null && list.size() > 0){
			result = list.get(0);			
		}
		return result;
	}
	

}
